package org.testfire.translator.watij;

import java.util.Map;
import org.testfire.model.Engine;
import org.testfire.model.TestType;
import org.testfire.model.TestsetType;
import org.testfire.model.VersionType;
import org.testfire.translator.TestFireTranslator;
import org.testfire.translator.TranslatorFactory;
import org.testfire.util.DomUtil;
import org.testfire.util.WatijUtils;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 2, 2007
 * Time: 3:52:34 PM
 *
 */
public class TestsetToWatijTranslator<T> implements TestFireTranslator<TestsetType>
{
    /**
     * Translate a TestSet xml object to a block of Java code.
     * @return The Java source code..
     */
    public Object translate( TestsetType testset, TestsetType testFireEntity )
    {
        String jName = testFireEntity.getName().replaceAll( " ", "" );
        StringBuilder buf = new StringBuilder( 1000 ); // not so big. kjfhskj


        buf.append( String.format( "/** Description:  %1$s */\n\t", testFireEntity.getDescription() ) );
        buf.append( String.format( "/** Created: %1$s */\n\t", DomUtil.DATE_FORMAT.format( testFireEntity.getCreated() ) ) );
        buf.append( String.format( "/** Last Verified: %1$s */\n\t", DomUtil.DATE_FORMAT.format( testFireEntity.getLastVerified() ) ) );
        buf.append( String.format( "/** For Bug: %1$s */\n\t", testFireEntity.getForBug() ) );
        TestFireTranslator translator = TranslatorFactory.get( VersionType.class);
        buf.append( translator.translate( testset, testFireEntity.getMinVersion() ));
        buf.append( translator.translate( testset, testFireEntity.getMaxVersion() ));

        buf.append( WatijUtils.getTestFileHeader( testFireEntity.getClassPackage(), jName ) ).append( "\n\n" );

        translator = TranslatorFactory.get( Engine.class);

        StringBuffer buf2 = new StringBuffer(100000); // big



        buf2.append( translator.translate( testset, testFireEntity.getEngine()));

        translator = TranslatorFactory.get( TestType.class);

        buf2.append( translator.translate( testset, testFireEntity.getSetUp() ));
        for ( TestType t : testFireEntity.getTests() )
        {
            buf2.append( translator.translate( testset, t ) );
        }
        buf2.append( translator.translate( testset, testFireEntity.getTearDown() ));
        buf2.append( " \n}\n" ); // close the class file.

        Map<String, String> varKeys = testset.getVariableKeys();
        for(String key : varKeys.keySet()){
            buf.append( varKeys.get(key)).append( " " ).append( key ).append( ";\n");
        }



        return buf.toString() + buf2.toString();
    }
}
